<template>
	<view class="container">
		<!-- 快递信息表单 -->
		<view class="form-box">
			<view class="form-item">
				<text class="label">取件地点</text>
				<view class="location-select" @tap="selectPickupLocation">
					<text>{{formData.pickupLocation || '请选择取件地点'}}</text>
					<uni-icons type="right" size="16" color="#999"></uni-icons>
				</view>
			</view>
			
			<view class="form-item">
				<text class="label">快递公司</text>
				<picker mode="selector" :range="expressCompanies" @change="onCompanyChange">
					<view class="picker">
						<text>{{formData.company || '请选择快递公司'}}</text>
						<uni-icons type="right" size="16" color="#999"></uni-icons>
					</view>
				</picker>
			</view>
			
			<view class="form-item">
				<text class="label">物品信息</text>
				<input type="text" v-model="formData.itemInfo" placeholder="请简要描述物品信息" />
			</view>
			
			<view class="form-item">
				<text class="label">预估重量 (kg)</text>
				<input type="digit" v-model="formData.weight" placeholder="请输入预估重量" />
			</view>
			
			<view class="form-item">
				<text class="label">期望取件时间</text>
				<picker mode="time" :value="formData.expectedTime" @change="onTimeChange">
					<view class="picker">
						<text>{{formData.expectedTime || '请选择时间'}}</text>
						<uni-icons type="right" size="16" color="#999"></uni-icons>
					</view>
				</picker>
			</view>
			
			<view class="form-item">
				<text class="label">备注信息</text>
				<textarea v-model="formData.remark" placeholder="请输入备注信息（选填）" />
			</view>
			
			<view class="price-section">
				<view class="price-item">
					<text class="label">跑腿费</text>
					<text class="price">¥{{basePrice}}</text>
				</view>
				<view class="price-item">
					<text class="label">加急费</text>
					<switch :checked="formData.isUrgent" @change="onUrgentChange" color="#2979ff" />
				</view>
				<view class="price-item">
					<text class="label">预估快递费</text>
					<text class="price">¥{{estimatedExpressFee}}</text>
				</view>
				<view class="price-item total">
					<text class="label">总计</text>
					<text class="price">¥{{totalPrice}}</text>
				</view>
			</view>
		</view>
		
		<!-- 发布按钮 -->
		<view class="submit-btn" @tap="submitOrder">
			发布订单
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			expressCompanies: ['顺丰快递', '京东快递', '中通快递', '圆通快递', '韵达快递', '申通快递'],
			formData: {
				pickupLocation: '',
				company: '',
				itemInfo: '',
				weight: '',
				expectedTime: '',
				remark: '',
				isUrgent: false
			},
			basePrice: 8,
			urgentFee: 5
		}
	},
	computed: {
		estimatedExpressFee() {
			if (!this.formData.weight || !this.formData.company) return 0
			// 这里简单模拟快递费计算，实际应该根据快递公司的计价规则计算
			const weight = parseFloat(this.formData.weight)
			const baseExpressFee = 15
			const additionalFee = Math.max(0, weight - 1) * 8
			return baseExpressFee + additionalFee
		},
		totalPrice() {
			return this.basePrice + 
				(this.formData.isUrgent ? this.urgentFee : 0) + 
				this.estimatedExpressFee
		}
	},
	methods: {
		selectPickupLocation() {
			uni.showActionSheet({
				itemList: ['宿舍楼', '教学楼', '图书馆', '食堂'],
				success: (res) => {
					this.formData.pickupLocation = ['宿舍楼', '教学楼', '图书馆', '食堂'][res.tapIndex]
				}
			})
		},
		onCompanyChange(e) {
			this.formData.company = this.expressCompanies[e.detail.value]
		},
		onTimeChange(e) {
			this.formData.expectedTime = e.detail.value
		},
		onUrgentChange(e) {
			this.formData.isUrgent = e.detail.value
		},
		submitOrder() {
			// 表单验证
			if (!this.formData.pickupLocation) {
				uni.showToast({
					title: '请选择取件地点',
					icon: 'none'
				})
				return
			}
			if (!this.formData.company) {
				uni.showToast({
					title: '请选择快递公司',
					icon: 'none'
				})
				return
			}
			if (!this.formData.itemInfo) {
				uni.showToast({
					title: '请输入物品信息',
					icon: 'none'
				})
				return
			}
			if (!this.formData.weight) {
				uni.showToast({
					title: '请输入预估重量',
					icon: 'none'
				})
				return
			}
			if (!this.formData.expectedTime) {
				uni.showToast({
					title: '请选择期望取件时间',
					icon: 'none'
				})
				return
			}
			
			// TODO: 发送请求保存订单
			uni.showLoading({
				title: '发布中...'
			})
			
			setTimeout(() => {
				uni.hideLoading()
				uni.showToast({
					title: '发布成功',
					icon: 'success'
				})
				// 返回首页
				uni.switchTab({
					url: '/pages/index/index'
				})
			}, 1500)
		}
	}
}
</script>

<style lang="scss">
.container {
	min-height: 100vh;
	background-color: $background-color;
	padding-bottom: 120rpx;
}

.form-box {
	background-color: #fff;
	padding: 20rpx;
	margin-top: 20rpx;
	
	.form-item {
		margin-bottom: 30rpx;
		
		.label {
			font-size: 28rpx;
			color: $text-color;
			margin-bottom: 20rpx;
			display: block;
		}
		
		input, textarea, .picker, .location-select {
			width: 100%;
			background-color: #f5f5f5;
			padding: 20rpx;
			border-radius: 8rpx;
			font-size: 28rpx;
			color: $text-color;
		}
		
		textarea {
			height: 160rpx;
		}
		
		.picker, .location-select {
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
	}
}

.price-section {
	background-color: #fff;
	padding: 20rpx;
	margin-top: 20rpx;
	
	.price-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
		
		.label {
			font-size: 28rpx;
			color: $text-color;
		}
		
		.price {
			font-size: 32rpx;
			color: #ff5500;
			font-weight: bold;
		}
		
		&.total {
			margin-top: 30rpx;
			padding-top: 20rpx;
			border-top: 1rpx solid $border-color;
		}
	}
}

.submit-btn {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	height: 100rpx;
	background-color: $theme-color;
	color: #fff;
	font-size: 32rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}
</style> 